package com.lx.response;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@NoArgsConstructor
@ToString
public class Result extends HashMap<String, Object> {

  private Result(Integer status, String message, Object data, Map<String, Object> extensions) {
    super();
    this.put("status", status);
    this.put("message", message);
    this.put("data", data);
    this.putAll(extensions);
  }

  public static ResultBuilder builder() {
    return new ResultBuilder();
  }

  public static class ResultBuilder {
    private ResultStatus status;
    private Object data;
    private Map<String, Object> extensions = new HashMap<>();

    private ResultBuilder() {
    }

    public ResultBuilder status(ResultStatus status) {
      this.status = status;
      return this;
    }

    public ResultBuilder data(Object data) {
      this.data = data;
      return this;
    }

    public ResultBuilder extension(String key, Object value) {
      this.extensions.put(key, value);
      return this;
    }

    public Result build() {
      return new Result(status.getCode(), status.getMessage(), data, extensions);
    }
  }
}
